package com.hnichr.ping.sms.channel.impl;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.hnichr.ping.common.DictEnum;
import com.hnichr.ping.common.vo.MobileResultVo;
import com.hnichr.ping.common.vo.SendResultVo;
import com.hnichr.ping.common.vo.SmsSendVo;
import com.hnichr.ping.sms.entity.SmsChannel;
import com.hnichr.ping.sms.channel.SmsChannelProvider;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class HnicSmsChannelProvider implements SmsChannelProvider {
    private static final Logger logger = LoggerFactory.getLogger(HnicSmsChannelProvider.class);

    private static final String TYPE = "dmas";
    private static final String PROVIDER_CODE = "hnic";

    @Override
    public String getProviderCode() {
        return PROVIDER_CODE ;
    }

    @Override
    public SendResultVo paramVerify(SmsSendVo smsSendVo){
        if(StringUtils.isBlank(smsSendVo.getSmsContent())){
            return SendResultVo.failed("smsContent不能为空");
        }
        return SendResultVo.success();
    }

    @Override
    public SendResultVo send(SmsChannel channel, SmsSendVo smsSendVo) {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("type", TYPE);
        paramMap.put("appId", channel.getPrincipal());
        paramMap.put("mobiles", smsSendVo.getMobiles());
        paramMap.put("content", smsSendVo.getSmsContent());
        try (HttpResponse response = HttpUtil.createPost(channel.getUrl()).contentType("application/json")
                .body(JSON.toJSONString(paramMap)).execute()) {
            String postReturn = response.body();
            //科服的接口，只要有一个发送成功，就返回0
            if (StringUtils.equals(postReturn, "0")) {
                SendResultVo sendResultVo = SendResultVo.success();
                for(String mobile:smsSendVo.getMobiles().split(",")){
                    MobileResultVo vo = new MobileResultVo();
                    vo.setMobile(mobile);
                    vo.setResultCode(DictEnum.SEND_RESULT.OK.getCode());
                    sendResultVo.getResult().add(vo);
                }
                return sendResultVo;
            }
            return SendResultVo.failed("发送失败");
        } catch (Exception e) {
            logger.error("发送短信失败", e);
            return SendResultVo.failed(e.getMessage());
        }
    }

    public static void main(String [] arg){
        String sms = "请收到此消息的员工立刻选择原地爆炸，谢谢！";
        String [] mobiles = new String[]{"19937178835"};
        //String [] userIds = new String[]{"146121"};
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("type", "ddmessage");
        //paramMap.put("appId","SqZD8TSFSvWdx34igpKrVg");
        paramMap.put("appId", "y_Tuxn6PQHut6gBsThFsNw");
        paramMap.put("phones", mobiles);
        //paramMap.put("userIds", userIds);
        paramMap.put("content", sms);
        paramMap.put("msgtype","link");
        paramMap.put("messageUrl","https://www.baidu.com/");
        paramMap.put("title","测试标题");
        paramMap.put("picUrl","https://img1.baidu.com/it/u=225041176,855892897&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=1422");
        paramMap.put("text","百度");
        String postReturn = "";
        try (HttpResponse response = HttpUtil.createPost("http://api.zt.hnic.com.cn/hnic/v1/message/sendHnicMessage").contentType("application/json")
                .body(JSON.toJSONString(paramMap)).execute()) {
            postReturn = response.body();
        }
        catch (Exception e){
            postReturn = e.getMessage();
        }
        System.out.println("postReturn>>>>>>>>>"+postReturn);

    }
}
